package cn.jsj.invite.controller;

import cn.jsj.invite.pojo.EnterpriseEnt;
import cn.jsj.invite.pojo.InvitationInfo;
import cn.jsj.invite.pojo.StudentEnt;
import cn.jsj.invite.result.Result;
import cn.jsj.invite.service.EnterpriseService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@CrossOrigin
public class EnterpriseController {

    private final EnterpriseService enterpriseService;

    public EnterpriseController(EnterpriseService enterpriseService) {
        this.enterpriseService = enterpriseService;
    }

    //企业注册
    @PostMapping("/enterprise/register")
    public Result<?> enterpriseRegister(EnterpriseEnt info) {
        if (enterpriseService.register(info) != 1) {
            return Result.fail("添加失败");
        }
        return Result.success();
    }

    //企业发帖
    @PostMapping("/enterprise/publishPage")
    public Result<?> enterprisePublishPage(InvitationInfo info,@RequestParam("tagList[]") String[] tagList,@RequestParam("file[]") MultipartFile[] file) {
        if (info == null) {
            return Result.fail("文字信息为空");
        }
        if (file == null) {
            return Result.fail("文件为空");
        }
        if (file.length == 0){
            return Result.fail("文件数量为0");
        }
        if (tagList == null){
            return Result.fail("标签信息为空");
        }
        if (tagList.length == 0){
            return Result.fail("标签数量为0");
        }
        try {
            String join = String.join(",", tagList);
            info.setTagList(join);
        } catch (Exception e){
            return Result.fail(e.getMessage() + "\r\n序列化标签异常");
        }
        if (enterpriseService.publishEnterpriseInvitation(info, file) != 1) {
            return Result.fail("添加失败");
        }
        return Result.success();
    }

    //根据企业ID查看企业的帖子
    @GetMapping("/enterprise/pages/{enterpriseId}")
    public Result<?> enterprisePage(@PathVariable Integer enterpriseId) {
        List<InvitationInfo> invitationInfoList = enterpriseService.listEnterpriseInvitation(enterpriseId);
        return Result.success(invitationInfoList);
    }

    //企业修改密码
    @PostMapping("/enterprise/updatePassword")
    public Result<String> modifyPasswordById(@RequestParam String enterpriseOldPassword, @RequestParam String enterpriseNewPassword, @RequestParam int enterpriseId) {
        if (enterpriseOldPassword != null && enterpriseNewPassword != null && enterpriseId > 0) {
            if (enterpriseService.updatePasswordById(enterpriseOldPassword, enterpriseNewPassword, enterpriseId) == 1) {
                return Result.success("1");
            } else {
                return Result.error("对不起，请检查数据是否填写正确");
            }
        } else {
            return Result.error("0");
        }
    }

    //企业查看有意向与企业联系的学生
    @GetMapping("/interest/students")
    public Result<?> interestStudents(Integer schoolId, Integer pageNumber) {
        PageInfo<StudentEnt> studentEntPageInfo = enterpriseService.listStudentIsOpen(schoolId, pageNumber);
        return Result.success(studentEntPageInfo);
    }

    //企业删除帖子
    @PostMapping("/enterprise/delete/page")
    public Result<?> enterpriseDeletePage(Integer pageId) {
        Integer integer = enterpriseService.removeOnePageById(pageId);
        if (integer == null) {
            return Result.fail("参数缺失");
        }
        if (integer.equals(0)) {
            return Result.fail("删除失败");
        }
        return Result.success();
    }

    //企业人员修改联系方式和公司介绍
    @PostMapping("/enterprise/update/info")
    public Result<?> enterpriseUpdateInfo(EnterpriseEnt info){
        if (info == null){
            return Result.fail("信息为空");
        }
        Integer integer = enterpriseService.updateEnterpriseInfo(info);
        if (integer == null){
            return Result.fail("账号信息有误");
        }
        if (integer.equals(1)){
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");

    }


}
